package com.user.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.achievement.entity.Course;
import com.achievement.entity.Score;
import com.achievement.entity.dto.CourseDTO;
import com.achievement.entity.dto.ScoreDTO;
import com.achievement.entity.dto.StuCourseDTO;
import com.achievement.service.ICourseService;
import com.achievement.service.IScoreService;
import com.common.util.AjaxResponse;
import com.common.util.JpaPageable;
import com.common.util.TablePage;
import com.exam.entity.Exam;
import com.exam.service.IExamService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mark.entity.StuAnswer;
import com.mark.service.IStuAnswerService;
import com.paper.entity.Paper;
import com.user.entity.Student;
import com.user.entity.Teacher;
import com.user.entity.dto.StudentDTO;
import com.user.service.IStudentService;

@Controller
@RequestMapping("/student")
public class StudentController {
	@Autowired
	private IStudentService studentService;
	@Autowired
	private ICourseService courseService;
	@Autowired
	private IExamService examService;
	@Autowired
	private IStuAnswerService stuAnswerService;
	@Autowired
	private IScoreService scoreService;
	
	@RequestMapping("/login")
	public @ResponseBody AjaxResponse login(String num,String password,HttpSession session) {
		try {
			StudentDTO dto = studentService.findLogin(num, password);
			if(dto == null) {
				return new AjaxResponse(false, "用户名或密码错误!");
			}else {
				session.setAttribute("student", dto);
				return new AjaxResponse(true, "登陆成功!");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new AjaxResponse(false, "登陆失败...");
		}
	}
	@RequestMapping("/logout")
	public void logout(HttpSession session,HttpServletResponse response) {
		try {
			session.removeAttribute("student");
			response.sendRedirect("/exam/stu_login");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	@RequestMapping("/findAll")
	public @ResponseBody List<StudentDTO> findAll(){
		try {
			return studentService.findAll();
		} catch (Exception e) {
			return null;
		}
	}
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody AjaxResponse saveOrUpdate(String student) {
		try {
			ObjectMapper objectMapper = new ObjectMapper();
			Student obj = objectMapper.readValue(student,Student.class);
			if(studentService.isNumExist(obj))
				return new AjaxResponse(false, "学号已存在！");
			studentService.saveOrUpdate(obj);
			return new AjaxResponse(true, "操作成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResponse(false, "操作失败...");
		}
	}
	@RequestMapping("/delete")
	public @ResponseBody AjaxResponse delete(Long id) {
		try {
			if(studentService.delete(id)) {
				return new AjaxResponse(true, "删除成功！");
			}else {
				return new AjaxResponse(false, "操作失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResponse(false, "操作失败！");
		}
	}
	@RequestMapping("/findExam")
	public @ResponseBody List<Exam> findExam(HttpSession session){
		try {
			StudentDTO dto = (StudentDTO) session.getAttribute("student");
			if(dto==null)return null;
			return studentService.findExam(dto.getId());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/findCourses")
	public @ResponseBody TablePage<Course> findCourses(HttpSession session,JpaPageable pageable){
		try {
			StudentDTO student = (StudentDTO) session.getAttribute("student");
			if(student == null)return null;
			if(student.getId()!=null) {
				return studentService.findCourses(CourseDTO.getSpecification(student.getId(),true), pageable.getPageable());
			}
			else {
				return null;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/findElective")
	public @ResponseBody TablePage<StuCourseDTO> findElective(HttpSession session,JpaPageable pageable){
		try {
			StudentDTO student = (StudentDTO) session.getAttribute("student");
			if(student == null)return null;
			return courseService.findStuDTOPage(student.getId(), StuCourseDTO.getSpecification(), pageable.getPageable());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/selectCourse")
	public @ResponseBody AjaxResponse selectCourse(HttpSession session,Long cId) {
		try {
			StudentDTO student = (StudentDTO) session.getAttribute("student");
			if(student==null)return new AjaxResponse(false, "未登陆!");
			courseService.selectCourse(student.getId(), cId);
			return new AjaxResponse(true, "选课成功!");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new AjaxResponse(false, "操作失败!");
		}
	}
	@RequestMapping("/attendExam")
	public @ResponseBody Exam attendExam(HttpSession session,Long eId) {
		try {
			StudentDTO student = (StudentDTO) session.getAttribute("student");
			if(null==student) {
				return null;
			}else {
				return examService.attendExam(student.getId(), eId);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping(value="/startExam",produces="application/json;charset=utf-8")
	public @ResponseBody String startExam(HttpSession session,Long exam_id) {
		//返回json格式:{'success':boolean,'msg','','paper':{},'stuAnswer':{}}
		try {
			return examService.startExam(session, exam_id);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return "{\"success\":false,\"msg\":\"参加失败...\"}";
		}
	}
	@RequestMapping("/findPage")
	public @ResponseBody TablePage<Student> findPage(JpaPageable pageable,String str,Long dept_id) {
		try {
			System.out.println(str+","+dept_id);
			return studentService.findPage(StudentDTO.getSpecification(str, dept_id), pageable.getPageable());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/activate")
	public @ResponseBody AjaxResponse activate(Long id) {
		try {
			studentService.activate(id);
			return new AjaxResponse(true, "操作成功!");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new AjaxResponse(false, "操作失败...");
		}
	}
	@RequestMapping("/revoked")
	public @ResponseBody AjaxResponse revoked(Long id) {
		try {
			if(studentService.hasCourse(id))
				return new AjaxResponse(false, "学生已选择课程，不允许注销用户!");
			studentService.revoked(id);
			return new AjaxResponse(true, "操作成功!");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new AjaxResponse(false, "操作失败...");
		}
	}
	@RequestMapping("/findScorePage")
	public @ResponseBody TablePage<Score> findScorePage(HttpSession session,JpaPageable pageable,Long course_id){
		try {
			StudentDTO dto = (StudentDTO) session.getAttribute("student");
			List<Long> ids = new ArrayList<Long>();
			Long stu_id = null;
			if(null!=dto) {
				ids = studentService.getCourseIds(dto.getId());
				stu_id = dto.getId();
			}
			return scoreService.findPage(ScoreDTO.getSpecification(ids, null, stu_id, course_id), pageable.getPageable());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/getCourseList")
	public @ResponseBody List<Course> getCourseList(HttpSession session){
		try {
			StudentDTO dto = (StudentDTO) session.getAttribute("student");
			if(null!=dto) {
				return studentService.getCourseList(dto.getId());
			}else {
				return courseService.findAll();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
}
